package com.currency.mall.ui.main.optional;

import com.currency.mall.Constant;
import com.currency.mall.entity.CollectShareId;
import com.currency.mall.entity.SecurityBean;
import com.currency.mall.entity.SharesBean;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.ui.main.optional.OptionalContract;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import java.util.HashMap;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/26.
 */

public class OptionalPresent implements OptionalContract.Presenter {
    private OptionalContract.View mView;

    public OptionalPresent(OptionalContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void collectSharesInfo(String sharesId) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("sharesId",sharesId);
        NetWork.getSharesApi().collectSharesInfo(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.finishRefresh();
                        if(responseBean.getCode().equals(Constant.SUCCESS_CODE)){
                            ToastUitl.show("收藏成功",200);
                            mView.refresh();
                        }else if(responseBean.getCode().equals("9112")){
                            PreferenceUtils.clearLoginInfo();
                            mView.toLogin();
                        }else{
                            ToastUitl.show(responseBean.getMsg(),200);
                        }
                    }
                });
    }

    @Override
    public void cancelCollectShares(String sharesId) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("sharesId",sharesId);
        NetWork.getSharesApi().cancelCollectShares(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean>bindToLife())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.finishRefresh();
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        mView.finishRefresh();
                        if(responseBean.getCode().equals(Constant.SUCCESS_CODE)){
                            ToastUitl.show("取消收藏",200);
                            mView.refresh();
                        }else if(responseBean.getCode().equals("9112")){
                                PreferenceUtils.clearLoginInfo();
                                mView.toLogin();
                        } else{
                            ToastUitl.show(responseBean.getMsg(),200);
                        }
                    }
                });
    }

    @Override
    public void getCollectShares() {
        NetWork.getSharesApi().collectList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<SharesBean>bindToLife())
                .subscribe(new Observer<SharesBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                       mView.finishRefresh();
                    }

                    @Override
                    public void onNext(SharesBean collectSharesBean) {
                        mView.finishRefresh();
                        if (collectSharesBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            mView.setSharesData(collectSharesBean.getCollectShares(),true);
                        }else if(collectSharesBean.getCode().equals("9112")){

                        } else {
                            ToastUitl.show(collectSharesBean.getMsg(), 200);
                        }
                    }
                });
    }

    @Override
    public void getCollectSharesId(final boolean isPolling) {
        NetWork.getSharesApi().getCollectShareId()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<CollectShareId>bindToLife())
                .subscribe(new Observer<CollectShareId>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.collectSharesId(new String[0]);
                    }

                    @Override
                    public void onNext(CollectShareId collectShareId) {
                        if(collectShareId.getCode().equals(Constant.SUCCESS_CODE)){
                            int collectShareIds[] = collectShareId.getCollectShareIds();
                            String str[] = new String[collectShareIds.length];
                            for(int i = 0;i< collectShareIds.length;i++){
                                str[i] = collectShareIds[i]+"";
                            }
                            mView.collectSharesId(str);
                        }else if(collectShareId.getCode().equals("9112")){
                            if(!isPolling){
                                PreferenceUtils.clearLoginInfo();
                                mView.toLogin();
                            }
                        } else{
                            mView.collectSharesId(new String[0]);
                            ToastUitl.show(collectShareId.getMsg(),200);
                        }

                    }
                });
    }
}
